package org.openkanban.server.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dozer.DozerBeanMapper;
import org.openkanban.server.interceptor.annotation.SecurityInterception;
import org.openkanban.server.service.KanbanService;
import org.openkanban.shared.dto.KanbanDto;
import org.openkanban.shared.dto.StatusDto;
import org.openkanban.shared.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Controller that is responsible for all kanban requests. Calls corresponding
 * services to proceed.
 */
@Controller
@RequestMapping(value = "/kanban")
public class KanbanController {

    @Autowired
    private KanbanService kanbanService;

    @Autowired
    private DozerBeanMapper mapper;

    @RequestMapping(method = RequestMethod.POST, value = "/{kanbanId}")
    @ResponseBody
    @SecurityInterception
    public KanbanDto getKanban(@PathVariable Long kanbanId, HttpServletRequest req, HttpServletResponse resp) {
	return mapper.map(kanbanService.getKanban(kanbanId), KanbanDto.class);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/update")
    @ResponseBody
    @SecurityInterception
    public KanbanDto updateKanban(KanbanDto kanbanDto, HttpServletRequest req, HttpServletResponse resp) {
	return mapper.map(kanbanService.updateKanban(kanbanDto), KanbanDto.class);
    }

    @RequestMapping(method = RequestMethod.POST, value = "{kanbanId}/user")
    @ResponseBody
    @SecurityInterception
    public UserDto[] getUser(@PathVariable Long kanbanId, HttpServletRequest req, HttpServletResponse resp) {
	return kanbanService.getUser(kanbanId);
    }

    @RequestMapping(method = RequestMethod.POST, value = "{kanbanId}/remove")
    @ResponseBody
    @SecurityInterception
    public StatusDto removeKanban(@PathVariable Long kanbanId, HttpServletRequest req, HttpServletResponse resp) {
	return kanbanService.removeKanban(kanbanId);
    }

}